package com.zijidelu.luframework.infrastructure.factory.impl;

import com.zijidelu.luframework.domain.model.entity.EntityRelation;
import com.zijidelu.luframework.infrastructure.factory.AggregateVault;
import com.zijidelu.luframework.utils.cache.vault.AbstractGroupedVault;
import com.zijidelu.luframework.utils.cache.vault.ItemVault;
import com.zijidelu.luframework.utils.cache.vault.SimpleVaultGroup;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 聚合根缓存实现类。
 *
 * @author ZIJIDELU
 * @datetime 2025/11/19 23:26
 */
@Component
public class AggregateVaultImpl extends AbstractGroupedVault<SimpleVaultGroup, Object, Object> implements AggregateVault {


    /**
     * 子实体Class->父实体Class映射字段
     *
     * @return 所有子实体类型映射缓存
     */
    private ItemVault<Class<?>, EntityRelation> childrenEntityClassesMap() {
        return getItem(SimpleVaultGroup.DEFAULT).selfCast();
    }

    /**
     * 父实体Class->子实体Class映射缓存
     *
     * @return 所有父实体类型映射缓存
     */
    private ItemVault<Class<?>, Set<EntityRelation>> rootEntityClassesMap() {
        return getItem(SimpleVaultGroup.GROUP_0).selfCast();
    }

    /**
     * 注册父子实体关系
     *
     * @param rootEntityClass  父实体Class
     * @param childEntityClass 子实体Class
     */
    @Override
    public EntityRelation registerEntityRelation(final Class<?> rootEntityClass, final Class<?> childEntityClass) {
        if (Objects.isNull(rootEntityClass) || Objects.isNull(childEntityClass)) {
            return null;
        }

        final EntityRelation relation = EntityRelation.of(rootEntityClass, childEntityClass);

        // 维护子->父映射
        childrenEntityClassesMap().put(childEntityClass, relation);

        // 维护父->子映射
        rootEntityClassesMap().computeIfAbsent(rootEntityClass, k -> new HashSet<>()).add(relation);

        return relation;
    }

    @Override
    public EntityRelation registerEntityRelationWithChild(Class<?> childEntityClass) {
        return registerEntityRelation(EntityRelation.calcRootEntityType(childEntityClass), childEntityClass);
    }

    @Override
    public EntityRelation registerEntityRelationWithChildNoCheck(final Class<?> childEntityClass) {
        if (!EntityRelation.isChildEntity(childEntityClass)) {
            return null;
        }

        return registerEntityRelationWithChild(childEntityClass);
    }

    @Override
    public Optional<EntityRelation> getEntityRelation(final Class<?> childEntityClass) {
        final EntityRelation entityRelation = childrenEntityClassesMap().get(childEntityClass);
        if (entityRelation != null) {
            return Optional.of(entityRelation);
        }

        // 注册父->子关系
        return Optional.ofNullable(registerEntityRelationWithChild(childEntityClass));
    }

    @Override
    public Set<EntityRelation> getEntityRelationSet(final Class<?> rootEntityClass) {
        if (Objects.isNull(rootEntityClass)) {
            return Collections.emptySet();
        }

        final Set<EntityRelation> entityRelations = rootEntityClassesMap().get(rootEntityClass);
        return entityRelations != null ? Collections.unmodifiableSet(entityRelations) : Collections.emptySet();
    }

}
